package com.heima.user.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.wemedia.IWemediaClient;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.dtos.AuthDto;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.mapper.ApUserRealnameMapper;
import com.heima.user.service.ApUserRealnameService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserRealnameServiceImpl extends ServiceImpl<ApUserRealnameMapper, ApUserRealname> implements ApUserRealnameService {


    /**
     * 用户审核列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listApUserRealname(AuthDto dto) {
        //1.参数检查
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页参数效验
        dto.checkParam();
        //登陆人获取
        Page<ApUserRealname> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<ApUserRealname> queryWrapper = new LambdaQueryWrapper<>();
        if (dto.getStatus() != null) {
            queryWrapper.eq(ApUserRealname::getStatus, dto.getStatus());
        }
        page = page(page, queryWrapper);

        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());


        return responseResult;
    }

    /**
     * 用户审核失败
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFailApUserRealname(AuthDto dto) {
        //1.参数检查
        if (dto == null || dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.根据id查询用户
        ApUserRealname apUserRealname = this.getById(dto.getId());
        if (apUserRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //设置审核状态改为失败
        apUserRealname.setStatus((short) 2);
        //设置失败原因
        apUserRealname.setReason(dto.getMsg());
        //设置更新原因
        apUserRealname.setUpdatedTime(new Date());

        this.updateById(apUserRealname);


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 用户审核成功
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authPassApUserRealname(AuthDto dto) {
        //1.参数检查
        if (dto == null || dto.getId() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.根据id查询用户
        ApUserRealname apUserRealname = this.getById(dto.getId());
        if (apUserRealname == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //设置审核状态改为成功
        apUserRealname.setStatus((short) 9);

        //设置更新时间
        apUserRealname.setUpdatedTime(new Date());

        this.updateById(apUserRealname);


        //审核成功,自媒体人添加用户数据
        boolean flag = saveWmUser(apUserRealname);
        if (!flag) {
            //远程调用wemedia创建自媒体用户
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private IWemediaClient wemediaClient;

    /**
     * 远程调用wemedia创建自媒体用户
     *
     * @param apUserRealname
     * @return
     */
    private boolean saveWmUser(ApUserRealname apUserRealname) {

        //查询用户是否在APP端存在
        ApUser apUser = apUserMapper.selectById(apUserRealname.getUserId());
        if (apUser == null) {
            return false;
        }
        //存在自媒体端用户与APP端用户账号密码一致
        //查询自媒体端用户是否存在
        WmUser wmUser = wemediaClient.findWmUserByName(apUser.getName());
        if (wmUser == null) {
            //自媒体端不存在创建用户
            wmUser = new WmUser();
            BeanUtils.copyProperties(apUser,wmUser);
            wmUser.setApUserId(apUser.getId());
            wmUser.setCreatedTime( new Date());
            wmUser.setStatus(9);
            wemediaClient.saveWmUser(wmUser);
        }
        //修改APP端用户状态
        apUser.setFlag((short)1);
        apUserMapper.updateById(apUser);


        return true;
    }
}
